<template>
	<view style="width: 100%;height:80vh;">

		<a-row>
			<a-col>

			</a-col>
		</a-row>
		<AgGridVue class="ag-theme-balham" style="width: 100%;height:80vh" :rowData="dataList" :columnDefs="col"
			:defaultColDef="defCol"></AgGridVue>
		<view>
			<a-pagination show-size-changer :default-current="pagination.page" :total="pagination.total"
				@change="changePage" @showSizeChange="onShowSizeChange" :pageSizeOptions="sizeOptions" />
		</view>
	</view>
</template>

<script>
	import agdate from "./agdate.vue"
	import 'ag-grid-community/styles/ag-theme-balham.min.css';
	import {
		AgGridVue
	} from "ag-grid-vue";
	import 'ag-grid-enterprise';
	import {
		LicenseManager
	} from "ag-grid-enterprise";
	LicenseManager.prototype.validateLicense = function() {
		return true;
	};
	export default {
		components: {
			AgGridVue,
			agdate
		},
		data() {
			return {
				curPage: 1,
				sizeOptions: ["10", "50", "100", "150", "200"],
				col: [{
						field: 'username',
						headerName: '用户',
					},
					{
						field: 'mechanism_id',
						headerName: '机构',
						cellRenderer: (params) => {
							return this.merMap[params.value]
						}
					},
					{
						field: 'ip',
						headerName: '登陆IP'
					},
					{
						field: 'create_date',
						headerName: '登陆时间',
						cellRenderer: 'agdate'
					},
					{
						field: 'last_login_ip',
						headerName: '最后登陆IP'
					},
					{
						field: 'last_login_date',
						headerName: '最后登陆日期',
						cellRenderer: 'agdate'
					}, {
						field: 'register_env',
						headerName: '注册IP'
					},
				],
				defCol: {
					resizable: true,
				},
				dataList: [],
				pagination: {
					limit: 10,
					page: 1,
					total: 0,
					processSize: 0
				},
				merData: [],
				merMap: {}
			}
		},
		onLoad() {
			this.getAllMecList()

		},
		methods: {
			changePage(page, pageSize) {
				this.pagination.page = page
				this.getLogPage()
			},
			onShowSizeChange(current, size) {
				this.pagination.page = 1
				this.pagination.limit = size
				this.getLogPage()
			},
			async getLogPage() {
				uni.showLoading({
					title: 'loading...'
				})
				const db = uniCloud.database()
				const count = await db.collection('uni-id-log').where('type=="login"').count();
				this.pagination.total = count.result.total

				let skip = (this.pagination.limit * (this.pagination.page - 1))

				const temp =await db.collection('uni-id-log')
					.orderBy('create_date desc')
					.skip(skip)
					.limit(this.pagination.limit)
					.get()
				const userIdList = []
				for(let r of temp.result.data){
					userIdList.push(r.user_id)
				}
				
				const userTemp = await db.collection('uni-id-users')
					.where({
						_id:db.command.in(userIdList)
					})
					.field('last_login_date,_id,last_login_ip,mechanism_id,register_env,username')
					.get() 
				console.log(userTemp.result.data)
				return

				// const userTemp = db.collection('uni-id-users')
				// 	.field('last_login_date,_id,last_login_ip,mechanism_id,register_env,username')
				// 	.getTemp()

				// const res = await db.collection(temp, userTemp)
				// 	.skip(skip)
				// 	.limit(this.pagination.limit)
				// 	.get()

				const t = res.result.data
				for (let i = 0; i < t.length; i++) {
					if (t[i].user_id.length > 0) {
						const {
							last_login_date,
							_id,
							last_login_ip,
							mechanism_id,
							register_env,
							username
						} = t[i].user_id[0]
						if (register_env != undefined) {
							t[i][`register_env`] = register_env.client_ip ? register_env.client_ip : '暂无'
						}
						t[i]['last_login_date'] = last_login_date
						t[i][`last_login_ip`] = last_login_ip
						t[i][`mechanism_id`] = mechanism_id
						t[i][`username`] = username
					}
				}

				this.dataList = t
				this.curPage = this.pagination.page
				uni.hideLoading()
				// console.log(res.result.data)
			},
			async getAllMecList() {
				const db = uniCloud.database()
				uni.showLoading({
					title: '加载中...'
				})
				const responseTotal = await db.collection('mechanism').count()
				const size = responseTotal.result.total
				// const size = 10
				if (size > 0) {
					let total = size
					let processSize = 0
					let position = 1
					let allData = []
					while (processSize < total) {
						const responseData = await db.collection('mechanism')
							.field("name,key")
							.skip(processSize).limit(500).get()
						if (responseData.result.data.length > 0) {
							responseData.result.data.forEach((e) => {
								e.indexId = position++
								allData.push(e)
							})
						}
						processSize += responseData.result.data.length
					}
					this.merData = allData
					for (let r of this.merData) {
						this.merMap[`${r._id}`] = r.name
					}
					this.getLogPage()
				}
				uni.hideLoading()
			},
		}
	}
</script>

<style>

</style>